package com.king.collect;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.MapDifference;
import com.google.common.collect.Maps;

import static com.google.common.collect.Maps.newHashMap;

/**
 * @author taomk
 * @version 1.0
 * @since 15-1-25 下午10:51
 */
public class MapTest {

	public static void main(String [] args) {
		// Map(JDK1.6这么用)
		Map<String, Map<Long, List<String>>> jdkMap = new HashMap<String, Map<Long,List<String>>>();
		// Map(Guava这么用，JDK1.7已实现)
		Map<String, Map<Long, List<String>>> guavaMap = newHashMap();

		Map<String, String> mapA = newHashMap();
		mapA.put("key1", "val1");
		mapA.put("key2", "val2");
		mapA.put("key3", "val3");
		mapA.put("key4", "val4");
		mapA.put("key5", "val5");

		Map<String, String> mapB = newHashMap();
		mapB.put("key3", "val3");
		mapB.put("key4", "val4");
		mapB.put("key5", "val5");
		mapB.put("key6", "val6");
		mapB.put("key7", "val7");

		MapDifference<String, String> differenceMap = Maps.difference(mapA, mapB);
		System.out.println(differenceMap.areEqual());

		Map<String, MapDifference.ValueDifference<String>> entriesDiffering = differenceMap.entriesDiffering();
		Map<String, String> entriesOnlyOnLeft = differenceMap.entriesOnlyOnLeft();
		Map<String, String> entriesOnlyOnRight = differenceMap.entriesOnlyOnRight();
		Map<String, String> entriesInCommon = differenceMap.entriesInCommon();

		ImmutableMap<String,String> immutableMap = ImmutableMap.of("key1", "value1", "key2", "value2");
		ImmutableMap<String, Integer> immutableMap1 = ImmutableMap.of(
				"1", 1,
				"2", 2,
				"3", 3
		);

		/**
		 * 假设你所有的不是事先知道的一些常量.
		 * 比如说, 你要从一个数据库里读出一些基本信息保存在内存里. 用of()就不好使了, 这时候, 可以用builder模式:
		 */
		ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
		for (;;) {
			builder.put("a", 1);
			break;
		}
		ImmutableMap<String, Integer> map = builder.build();
	}
}
